package com.qf.controller;

import com.qf.pojo.DtsStorage;
import com.qf.service.DtsStorageService;
import com.qf.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/admin/storage")
public class DtsAdminStorageController {
    @Autowired
    DtsStorageService dtsStorageService;

    @GetMapping("/list")
    public Object list(@RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                       @RequestParam(value = "limit", required = false, defaultValue = "20") Integer limit,
                       String name, String key, String sort, String order) {
        Map<String, Object> data = dtsStorageService.queryPage(page, limit, name, key, sort, order);
        return ResponseUtil.ok(data);
    }

    @PostMapping("/create")
    public Object create(MultipartFile file) throws Exception {
        byte[] buf =file.getBytes();

        DtsStorage storage = new DtsStorage();
        //name：原始文件名
        storage.setName(file.getOriginalFilename());
        //type 文件类型
        storage.setType(file.getContentType());
        //size 文件大小
        storage.setSize((int)file.getSize());

        storage= dtsStorageService.save(storage, buf);
        //响应前端
        return ResponseUtil.ok(storage);
    }

}
